package ip.youtube;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import com.google.gdata.client.http.AuthSubUtil;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.mediarss.MediaPlayer;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.util.ServiceException;

public class SearchBean {

	private static final String developer_key = "AI39si4aEMMg1Cmp28MyfoZf5l19N9z0ZjiwEJ6Md7z8WhW7EAHxtENNgwgk5duVX_lrje9TH8OCIn3Zo6JAag_bMRyCqeKjxA";
	private static final String clientID ="";
	
	private String searchString;
	private ArrayList<String> videoListe;

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
	
	
	public ArrayList<String> getVideoListe() {
		return videoListe;
	}

	public void setVideoListe(ArrayList<String> videoListe) {
		this.videoListe = videoListe;
	}

	public ArrayList<String> fireYouTubeSearch(){
		videoListe = new ArrayList();
		String requestUrl =
				AuthSubUtil.getRequestUrl("localhost:80",
				                        "http://gdata.youtube.com",
				                        false,
				                        true);

		try {
		YouTubeService service = new YouTubeService(clientID,developer_key);
		
		YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
		query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT);

		query.setFullTextQuery(searchString);
		query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);

		VideoFeed videoFeed;
			videoFeed = service.query(query, VideoFeed.class);
			for(VideoEntry videoEntry : videoFeed.getEntries()){		
			    YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
			    MediaPlayer mediaPlayer = mediaGroup.getPlayer();
				videoListe.add(mediaPlayer.getUrl());
			}
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			System.out.println("Service funktioinert nicht");
			e.printStackTrace();
		}
		

	
		
		return videoListe;
	}
	
	
}
